'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
    async echo() {

    } 
    //注册
    async create() {
        try {
            console.log("1");
            // const body = { username: '李四', phone: '18215695874', password: '123456', face_url: 'sfgsegew' };
            const body = this.ctx.request.body;
            // const body=this.ctx.params;
            console.log("2");
            console.log(this.ctx.service.users);
            await this.ctx.service.users.createUser(body);
            // console.log(body);
            this.ctx.body = {
                code: 20000,
                message: true
            }
        } catch (error) {
            this.ctx.body = {
                code: 30000,
                data: "失败"
            }
        }
    }
    async getUserList() {
        console.log("111111111111");
        let result = await this.ctx.service.users.getUserList();
        // this.ctx.body="数据";
        // console.log(result);
        if (result) {
            this.ctx.body = {
                code: 20000,
                data: result
            }
        }
        else {
            this.ctx.body = {
                code: 50000,
                data: '数据获取失败'
            }
        }
    }
    async deleteUser() {
        try {
            const id = await this.ctx.params.id
            // const id = 2
            console.log(id)
            await this.ctx.service.users.deleteUser(id)
            this.ctx.body = {
                code: 20000,
                message: true,
            }
        } catch (error) {
            this.ctx.body = {
                code: 30000,
                message: false,
            }
        }
    }
    async update() {
        try {
            const body = await this.ctx.request.body
            const id = await this.ctx.params.id;
            console.log("789");
            // const body = { username: '李四', phone: '18215695874', password: '9999999999', face_url: 'sfgsegew' };
            console.log("987");
            // const id = 3;
            const user = await this.ctx.service.users.resetPassword(id, body.password);
            console.log("666");
            this.ctx.body = {
                code: 20000,
                message: true,
            }
        } catch (error) {
            this.ctx.body = {
                code: 30000,
                message: false,
            }
        }
    }

}

module.exports = UserController;
